//
//  DXCustomPickerVeiw.m
//  UILbrary
//
//  Created by mac on 16/9/27.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "DXCustomPickerVeiw.h"

static NSString *DXCustomPickerVeiwKEY = @"DXCustomPickerVeiwKEY";
@interface DXCustomPickerVeiw()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic, strong) UIPickerView *resolvingPickerVeiw;/// 分辨率选择器
@property (nonatomic, strong) UIView *bgView;///背景图层
@property (nonatomic, strong) UIPickerView *timePickerView;///时间选择器
@property (nonatomic, strong) UIButton *sureButton; ///确认按钮


@end


@implementation DXCustomPickerVeiw
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        [self configSubviews];
        
    }
    return self;
}

- (void)drawRect:(CGRect)rect{
    NSString *tips = @"滑动进行选择";
    NSDictionary *attributes = @{NSForegroundColorAttributeName:[UIColor lightGrayColor],
                                 NSFontAttributeName:[UIFont systemFontOfSize:17.0]};
    
    CGRect tipsRect = [tips boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 40) options:NSStringDrawingUsesFontLeading attributes:attributes context:nil];
    CGFloat tips_x = [UIScreen mainScreen].bounds.size.width/2.0 - tipsRect.size.width/2.0;
    CGFloat tips_y = [UIScreen mainScreen].bounds.size.height/2.0 - 100;
    [tips drawAtPoint:CGPointMake(tips_x, tips_y) withAttributes:attributes];
}

- (void)configSubviews{
    
    [self addSubview:self.resolvingPickerVeiw];
    [self addSubview:self.timePickerView];
    [self addSubview:self.sureButton];

}

- (void)show{
    [self addSubview:self.resolvingPickerVeiw];
    [self addSubview:self.sureButton];
}

+ (void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:[self shareInstance]];
}

+ (instancetype)shareInstance{
    
    static DXCustomPickerVeiw *pickerView;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        pickerView = [[DXCustomPickerVeiw alloc] initWithFrame:[UIScreen mainScreen].bounds];
    });
    return pickerView;
}


- (void)dismiss{

    //[self.resolvingPickerVeiw removeFromSuperview];
    //[self.sureButton removeFromSuperview];
    [self removeFromSuperview];
    
   
    NSInteger resolvingIndex = [self.resolvingPickerVeiw selectedRowInComponent:0];
    NSString *resolvingCurrentTitle = self.resolvingTitleArr[resolvingIndex];
    
    NSInteger timeIndex = [self.timePickerView selectedRowInComponent:0];
    NSString *timeCurrrentTitle = self.timeArr[timeIndex];
    
    self.sureBlock(resolvingCurrentTitle,timeCurrrentTitle);
}

#pragma mark PickVeiw delegater  datasource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (pickerView == self.resolvingPickerVeiw) {
        return self.resolvingTitleArr.count;
    }else{
        return self.timeArr.count;
    }

    return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
   
    if (pickerView == self.resolvingPickerVeiw) {
        
        return self.resolvingTitleArr[row];
    }else{
        return self.timeArr[row];
    }
    return @"";
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSLog(@"%ld",row);
}

#pragma mark - lazy

- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:self.bounds];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}


- (UIButton *)sureButton{
    if (!_sureButton) {
        
        CGFloat sureButtonWidth = 300;
        CGFloat sureButtonHeigh = 40;
        CGFloat sureButtonX = self.bounds.size.width/2.0 - sureButtonWidth/2.0;
        CGFloat sureButtonY = [UIScreen mainScreen].bounds.size.height/2.0 + 100;//后期还要调整
        _sureButton = [[UIButton alloc] initWithFrame:CGRectMake(sureButtonX, sureButtonY, sureButtonWidth, sureButtonHeigh)];
        [_sureButton setTitle:@"确定" forState:UIControlStateNormal];
        [_sureButton setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];
        [_sureButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _sureButton;
}

- (UIPickerView *)resolvingPickerVeiw{
    if (!_resolvingPickerVeiw) {
        
        CGFloat resolvingPickerViweX = 0;
        CGFloat resolvingPickerVeiwY =[UIScreen mainScreen].bounds.size.height/2.0 - 50 ;
        CGFloat resolvingPickerVeiwHeigh = 40;
        CGFloat resolvingPickerViewWith = [UIScreen mainScreen].bounds.size.width;
        _resolvingPickerVeiw = [[UIPickerView alloc] initWithFrame:CGRectMake(resolvingPickerViweX, resolvingPickerVeiwY, resolvingPickerViewWith, resolvingPickerVeiwHeigh)];
        _resolvingPickerVeiw.dataSource = self;
        _resolvingPickerVeiw.delegate = self;
        _resolvingPickerVeiw.showsSelectionIndicator = YES;///这是什么鬼
    }
    return _resolvingPickerVeiw;
}

- (UIPickerView *)timePickerView{
    if (!_timePickerView) {
        
        CGFloat timePickViewX = 0;
        CGFloat timePickViewHeigh = 40;
        CGFloat timePickVeiwWidth = [UIScreen mainScreen].bounds.size.width;
        CGFloat timePickViewY = [UIScreen mainScreen].bounds.size.height/2.0 + 10;
        _timePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(timePickViewX, timePickViewY, timePickVeiwWidth, timePickViewHeigh)];
        _timePickerView.dataSource = self;
        _timePickerView.delegate = self;
        _timePickerView.showsSelectionIndicator = NO;///
        
    }
    return _timePickerView;
}

//下面这两个数组，本是应该由外面传入， 现在写到里面为了方便测试；
- (NSArray *)resolvingTitleArr{
    if (!_resolvingTitleArr) {
        _resolvingTitleArr = @[@"320,240",@"640,480",@"800,600",@"1024,768"];
    }
    return _resolvingTitleArr;
}

- (NSArray *)timeArr{
    if (!_timeArr) {
        _timeArr = @[@"5",@"6",@"7",@"8",@"9",@"10"];
    }
    return _timeArr;
}
@end
